package ru.pb.dao.auth.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import ru.pb.auth.dao.ClanInfoDAO;
import ru.pb.auth.dao.InventoryInfoDAO;
import ru.pb.auth.dao.PlayerInfoDAO;
import ru.pb.global.dao.DAOManager;
import ru.pb.global.dao.annotations.DAO;
import ru.pb.global.data.model.ClanInfo;
import ru.pb.global.data.model.PlayerInfo;
import ru.pb.global.dbc.DatabaseFactory;
import ru.pb.global.dbc.DatabaseUtils;

/**
 * @author: Felixx
 */
@DAO(database = "MySQL")
public class PlayerInfoDAOImpl implements PlayerInfoDAO
{
	private static final Logger _log = Logger.getLogger(PlayerInfoDAOImpl.class);

	private ClanInfoDAO _clanInfoDao;
	private InventoryInfoDAO _itemsInfoDao;

	public void init()
	{
		_clanInfoDao = DAOManager.getDAOImpl(ClanInfoDAO.class);
		_itemsInfoDao = DAOManager.getDAOImpl(InventoryInfoDAO.class);
	}

	@Override
	public PlayerInfo getPlayerInfo(String acc)
	{
		Connection con = null;
		PreparedStatement statement = null;
		ResultSet rset = null;
		PlayerInfo info = new PlayerInfo();
		try
		{
			con = DatabaseFactory.getInstance().newConnection();
			statement = con.prepareStatement("SELECT * FROM players WHERE account=? LIMIT 1");
			statement.setString(1, acc);
			rset = statement.executeQuery();
			if(rset.next())
			{
				int player_id = rset.getInt("player_id");
				String playerName = rset.getString("player_name");
				int clan_id = rset.getInt("clan_id");

				if(clan_id > 0)
				{
					info.setClanInfo(_clanInfoDao.getClanInfo(clan_id, con));
				}
				else
				{
					info.setClanInfo(new ClanInfo());
				}

				info.setPlayerId(player_id);

				info.setPlayerName(playerName);

				info.setRank(rset.getInt("rank"));

				info.setGamePoint(rset.getInt("gp"));

				info.setExp(rset.getInt("exp"));

				info.setPcCafe(rset.getInt("pc_cafe"));

				info.setFights(rset.getInt("fights_s"), true);
				info.setFights(rset.getInt("fights_ns"), false);

				info.setWinFights(rset.getInt("fights_win_s"), true);
				info.setWinFights(rset.getInt("fights_win_ns"), false);

				info.setLostFights(rset.getInt("fights_lost_s"), true);
				info.setLostFights(rset.getInt("fights_lost_ns"), false);

				info.setKills(rset.getInt("kills_count_s"), true);
				info.setKills(rset.getInt("kills_count_ns"), false);

				info.setDeaths(rset.getInt("deaths_count_s"), true);
				info.setDeaths(rset.getInt("deaths_count_ns"), false);

				info.setEscapes(rset.getInt("escapes_s"), true);
				info.setEscapes(rset.getInt("escapes_ns"), false);

				info.setCreateDate(rset.getLong("create_date"));
				info.setLastAccess(rset.getInt("lastAccess"));
				info.setOnlineTime(rset.getInt("onlinetime"));
				info.setOnline(rset.getBoolean("online"));

				info.setInventoryInfo(_itemsInfoDao.getInventoryInfo(acc, con));

			}
		}
		catch(SQLException e)
		{
			_log.info("SQLException: " + e, e);
		}
		finally
		{
			DatabaseUtils.closeDatabaseCSR(con, statement, rset);
		}
		return info;
	}
}